package com.ljm.file.autoconfigure;

import com.ljm.file.properties.LjmFileProperties;
import com.ljm.file.service.FileService;
import com.ljm.file.service.impl.AliOssFileSerivceImpl;
import com.ljm.file.util.AliOssUtil;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: ChenHuaMing
 * @Date: 2020/9/28 11:08
 * @Description:
 */
@Configuration
@ConditionalOnProperty(
        name = {"ljm.oss.enabled"},
        matchIfMissing = true
)
@EnableConfigurationProperties(LjmFileProperties.class)
public class OssAutoConfiguration {

    @Bean
    public FileService fileService(LjmFileProperties ljmFileProperties){
        FileService fileService=null;
        switch (ljmFileProperties.getType()){
            case alibaba_oss:
                fileService=this.configAlibabOss(ljmFileProperties);
                break;
            case qiuniu_oss:
                break;
                default:
        }
        return fileService;
    }

    private FileService configAlibabOss(LjmFileProperties ljmFileProperties){
        AliOssUtil ossUtil=new AliOssUtil(ljmFileProperties);
        return new AliOssFileSerivceImpl(ljmFileProperties.getPublicBucketName(),ljmFileProperties.getSecrecyBucketName(),ossUtil);
    }
}
